import { IRootState } from '@/store/types';
import { Module } from 'vuex';
import { IDashBoardState } from './types';
import {
  getAddressGoodSale,
  getCategoryGoodFavor,
  getCategoryGoodCount,
  getCategoryGoodSale
} from '@/service/main/analysis/dashboard';

const dashboardModule: Module<IDashBoardState, IRootState> = {
  namespaced: true,
  state() {
    return {
      categoryGoodsCount: [],
      categoryGoodsSale: [],
      categoryGoodsFavor: [],
      addressGoodsSale: []
    };
  },
  mutations: {
    changeCategoryGoodsCount(state, list) {
      state.categoryGoodsCount = list;
    },
    changeCategoryGoodsSale(state, list) {
      state.categoryGoodsSale = list;
    },
    changeCategoryGoodsFavor(state, list) {
      state.categoryGoodsFavor = list;
    },
    changeAddressGoodsSale(state, list) {
      state.addressGoodsSale = list;
    }
  },
  actions: {
    async getDashBoardDataAction({ commit }) {
      const categoryGoodsCountList = await getCategoryGoodCount();
      commit('changeCategoryGoodsCount', categoryGoodsCountList.data);

      const categoryGoodsSaleList = await getCategoryGoodSale();
      commit('changeCategoryGoodsSale', categoryGoodsSaleList.data);

      const categoryGoodsFavorList = await getCategoryGoodFavor();
      commit('changeCategoryGoodsFavor', categoryGoodsFavorList.data);

      const addressGoodsSaleList = await getAddressGoodSale();
      commit('changeAddressGoodsSale', addressGoodsSaleList.data);
    }
  }
};

export default dashboardModule;
